package messenger;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.io.*;
import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.ScrollPaneConstants;

public class ChatWindowBuilder {    
           
    private JFrame frame;
    private JTextArea incoming;
    private JTextArea outgoing;
    private JPanel paineldeFundo;                   
    private JPanel mainPanel;
    private JPanel secondPanel;
    private JScrollPane aScroller;
    private JScrollPane qScroller;
    private JButton sendButton;        
    
    
    private BufferedReader entrada;
    private PrintWriter saida;
    private String localIp;
    private String hostIp;
    
    
    public ChatWindowBuilder(String title,BufferedReader entrada,PrintWriter saida, String localIp, String hostIp) {
        this.entrada = entrada;
        this.saida = saida;
        this.localIp = localIp;
        this.hostIp = hostIp;
        frame = new JFrame(title);
        paineldeFundo = new JPanel();        
        paineldeFundo.setLayout(new BorderLayout());   
        mainPanel = new JPanel();
        secondPanel = new JPanel();
    }
    
    public void CreateIncomingTextArea(){
        incoming = new JTextArea(15,35);
        incoming.setLineWrap(true);
        incoming.setWrapStyleWord(true);
        incoming.setEditable(false);        
        qScroller = new JScrollPane(incoming);
        qScroller.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
        qScroller.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
        qScroller.setBorder(BorderFactory.createLineBorder(Color.black));
        qScroller.revalidate();
    }
    
    public void CreateOutgoingTextArea(){
        outgoing = new JTextArea(5,28);
        outgoing.setToolTipText("type a message");                 
        outgoing.setLineWrap(true);
        outgoing.setWrapStyleWord(true);       
        outgoing.addKeyListener(new EnterListener());
        aScroller = new JScrollPane(outgoing);        
        aScroller.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
        aScroller.setBorder(BorderFactory.createLineBorder(Color.black));
    }
    
    public void CreateSendButton(){
        sendButton = new JButton("enviar");        
        sendButton.addActionListener(new SendButtonListener());    
    }
            
    public void BuildFrame(){
        mainPanel.add(qScroller);   
        secondPanel.add(aScroller); 
        secondPanel.add(sendButton);
        paineldeFundo.add(BorderLayout.CENTER,mainPanel);
        paineldeFundo.add(BorderLayout.SOUTH,secondPanel);
        frame.setContentPane(paineldeFundo);
        frame.pack();
        Image imagem = new ImageIcon("imagens\\messenger.jpg").getImage();        
        frame.setIconImage(imagem);        
        frame.setResizable(false);
    }
    
    public void CreateAll(){
        CreateIncomingTextArea();
        CreateOutgoingTextArea();
        CreateSendButton();
        BuildFrame();
    }
    
    public JFrame getFrame(){
        return frame;
    }

    public JTextArea getIncoming() {
        return incoming;
    }   
    
    public class EnterListener implements KeyListener{          
        private boolean auxiliar = false; // me auxiliará pra eu detectar o shift+enter no TextArea
         public void keyPressed(KeyEvent e) {             
            if(e.getKeyCode() == KeyEvent.VK_SHIFT){
                auxiliar = true;
            }
        }        
        
        public void keyTyped(KeyEvent e) {}

        public void keyReleased(KeyEvent e) {
            if (auxiliar == false && (e.getKeyCode() == KeyEvent.VK_ENTER) ){                               
                String[] semEnter = outgoing.getText().split("\\n");
                for(String s:semEnter){
                    saida.println(s);                   
                    saida.flush();
                    String[] str = localIp.split("/");
                    incoming.append(str[1]+" (me): " + s + "\n");                     
                }         
                outgoing.setText("");
                outgoing.requestFocus();
            }
            else if(e.getKeyCode() == KeyEvent.VK_SHIFT){
                auxiliar = false;
            }
            else if(auxiliar == true && (e.getKeyCode() == KeyEvent.VK_ENTER) ){
                outgoing.append("\n");
            }
        }               
     }  
     
    public class SendButtonListener implements ActionListener{
        public void actionPerformed(ActionEvent ev) {
            try{
                String st = outgoing.getText();                
                saida.println(st);                   
                saida.flush();
                String[] str = localIp.split("/");
                incoming.append(str[1]+" (me): " + st + "\n");
                
            }catch(Exception e){ e.printStackTrace();}            
            outgoing.setText("");
            outgoing.requestFocus();
        }    
    }
    
}